var tableIns;
layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

     tableIns = table.render({
        elem: '#caseType',
        url: '/caseType/getCaseTyepList',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 10,
        limits: [10, 15, 20, 25],
        id: "adminListTable",
        cols: [[
            {field: 'name', title: '名称', align: 'center'},
            {field: 'compName', title: '公司', align: 'center'},
            {field: 'isBindMobile', title: '是否必须绑定手机', align: 'center', templet: function (f) {
                    if (f.isBindMobile == 1) {
                        return "<span class='layui-red'>是</span>"
                    } else {
                        return "<span>否</span>"
                    }
                }
            },
            {field: 'isDefault', title: '是否默认', align: 'center', templet: function (f) {
            		if (f.isDefault == 1) {
            			return "<span class='layui-red'>是</span>"
            		} else {
            			return "<span>否</span>"
            		}
            	}
            },
            {title: '操作', templet: '#caseTypeBar', fixed: "right", width: 200, align: "center"}
        ]]
    });

    $(".addbtn").on("click", function () {
        var addindex = layui.layer.open({
            title: "新增类型",
            type: 2,
            content: '/caseType/add',
        });
        layui.layer.full(addindex)
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(addindex);
        });
        form.render();
    });

    form.on("select(selectComp)", function (data) {
        id = data.value;
        console.log(id);
        //执行重载
        table.reload('adminListTable', {
            url: '/caseType/getCaseTyepList'
            , page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                compId: id
            }
        });
    });

    function edit(edit) {
        // console.log(edit)
        var editindex = layui.layer.open({
            title: "修改类型",
            type: 2,
            content: '/caseType/add?typeId=' + edit.typeId,
        });
        layui.layer.full(editindex)
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(editindex);
        });
        form.render();
    }

    //列表操作
    table.on('tool(caseType)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'del') { //删除
            layer.confirm('确定删除此计费信息？', {icon: 3, title: '提示信息'}, function (index) {
                if (index) {
                    $.get("/caseType/deleteCaseTyep?typeId=" + data.typeId, function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            tableIns.reload();
                        } else {
                            layer.open({
                                title: '温馨提示'
                                , content: result.message
                            });
                            tableIns.reload();
                        }
                        ;
                    });

                } else {
                    tableIns.reload();
                }
                ;
            });
        } else if (layEvent === 'isDefault') {
            layer.confirm('设为默认后，以后的储物柜都会按照此收费标准收费，如需改动可手动修改？', {icon: 3, title: '提示信息'}, function (index) {
                if (index) {
                    $.get("/caseType/setDefault?typeId=" + data.typeId, function (result) {
                        if (result.success) {
                            layer.msg("设置成功");
                            tableIns.reload();
                        } else {
                            layer.open({
                                title: '温馨提示'
                                , content: result.message
                            });
                            tableIns.reload();
                        }
                        ;
                    });

                } else {
                    tableIns.reload();
                }
                ;
            });
        } else if (layEvent === 'edit') {
            // console.log(data)
            edit(data);
        }
        ;
    });

    var compId, roleId;
    $(".li3").on("click", function () {
        var comp = $(this).parent().parent();
        var a = comp.children().children('span').text();
        var b = $(this).text();

        var compId = comp.val();
        var roleId = $(this).val();
        $(".compId").val(compId);
        $(".roleId").val(roleId);
        // console.log(compId)
        // console.log(a)
        $("#test").val(a + "-" + b);
        $('.li3').hide();
    })
    $("#test").on("mouseover", function () {
        $('.li3').show();
    })

    /*    //添加操作
        form.on('submit(addFormAdd)',function(data){
            $.post("/main/userAdd",data.field,function(rest){
                if(rest.success){
                    layer.msg("添加成功");
                    parent.window.location.reload();
                }else{
                    layer.msg(rest.message);
                }
                })
         })
         form.on('submit(changeInfo)',function(data){
             $.post("/main/updateUserLogin",data.field,function(rest){
                 if(rest.success){
                     layer.msg("编辑成功");
                     parent.window.location.reload();
                 }else{
                     layer.msg(rest.message);
                 }
             })
         })*/

})